package com.example.accessingdatajpa.service.impl;

import com.example.accessingdatajpa.pojo.Goods;
import com.example.accessingdatajpa.repository.GoodsRepository;
import com.example.accessingdatajpa.response.RestResponse;
import com.example.accessingdatajpa.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.transaction.Transactional;
import java.util.List;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsRepository goodsRepository;

    //find all goods
    @Override
    @Transactional
    public RestResponse<List<Goods>> queryAllGoods(){
        RestResponse<List<Goods>> response = new RestResponse<>();
        try {
            List<Goods> data = goodsRepository.findAll();
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    //find goods by categoryId
    @Override
    @Transactional
    public RestResponse<List<Goods>> queryGoodsByCategoryId(Long categoryId){
        RestResponse<List<Goods>> response = new RestResponse<>();
        try {
            List<Goods> data = goodsRepository.findByCategory_CategoryId(categoryId);
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    //fuzzy search goods by goods title
    @Override
    @Transactional
    public RestResponse<List<Goods>> queryGoodsByGoodsName(String goodsTitle){
        RestResponse<List<Goods>> response = new RestResponse<>();
        try {
            List<Goods> data = goodsRepository.findByGoodsTitleContaining(goodsTitle);
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    //find hot goods
    @Override
    @Transactional
    public RestResponse<List<Goods>> queryHotGoods(){
        RestResponse<List<Goods>> response = new RestResponse<>();
        try {
            List<Goods> data = goodsRepository.findByHotGoods(1);
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    //find new goods
    @Override
    @Transactional
    public RestResponse<List<Goods>> queryNewGoods(){
        RestResponse<List<Goods>> response = new RestResponse<>();
        try {
            List<Goods> data = goodsRepository.findByNewGoods(1);
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    //find other goods
    @Override
    @Transactional
    public RestResponse<List<Goods>> queryOtherGoods(Long categoryId){
        RestResponse<List<Goods>> response = new RestResponse<>();
        try {
            List<Goods> data = goodsRepository.findByNewGoodsAndHotGoodsAndCategory_CategoryId(0,0,categoryId);
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }


    //add new goods
    @Override
    @Transactional
    public RestResponse<String> addNewGoods(Goods goods){
        RestResponse<String> response = new RestResponse<>();
        try {
            goodsRepository.save(goods);
            response.setStatus(200);
            response.setMessage("ok");
            response.setData("保存成功");
        }catch (RuntimeException e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            response.setStatus(500);
            response.setMessage(e.getMessage());
            response.setData("保存失败");
        }
        return response;
    }
}
